<?php

namespace App\Http\Controllers\Api\V1;

// use Illuminate\Http\Request;
use App\Http\Requests\Api\ApiRequest as Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use App\Exceptions\Interfaces\CouponServiceException;

use App\Models\UserCoupon;
use App\Models\Coupon;
use App\Interfaces\CouponService;
use Illuminate\Support\Collection;

class CouponController extends Controller
{


    /**
     * @api {post} /coupon/get_coupon 获取用户优惠券
     * @apiName Coupon/get_coupon
     * @apiGroup Coupon
     * @apiDescription 获取用户优惠券.
     *
     * @apiParam {String} coupon_status  用户优惠券的查询条件,默认为空:所有未删除的; can_use:未使用; used:已使用; expired:已失效; deleted:已删除.
     *
     * @apiSuccess {Object[]}  Object                       返回的用户优惠券信息
     * @apiSuccess {Number}  Object.id                      优惠券id
     * @apiSuccess {String}  Object.name                    优惠券名称
     * @apiSuccess {Number}  Object.value                   优惠券金额
     * @apiSuccess {Number}  Object.amount                  优惠券发放数量
     * @apiSuccess {Number}  Object.receive_amount          优惠券已经被领取的数量
     * @apiSuccess {Number}  Object.limit_fir               优惠券使用规则1，0：无限制；大于0：满多少使用
     * @apiSuccess {Number}  Object.type                    优惠券可用类型，1：全场通用；2：商品；3：分类；4：店铺；5：品牌
     * @apiSuccess {String}  Object.remarks                 优惠券备注说明
     * @apiSuccess {Number}  Object.created_at              优惠券创建时间
     * @apiSuccess {Number}  Object.start_time              优惠券开始时间
     * @apiSuccess {Number}  Object.end_time                优惠券过期时间
     * @apiSuccess {Number}  Object.updated_at              优惠券更新时间
     * @apiSuccess {Number}  Object.deleted_at              优惠券删除时间
     * @apiSuccess {Number}  Object.user_coupon.id          用户优惠券id
     * @apiSuccess {Number}  Object.user_coupon.use_time    用户优惠券使用的时间
     * @apiSuccess {Number}  Object.user_coupon_created_at  用户领取优惠券的时间
     * @apiSuccess {Number}  Object.user_coupon_deleted_at  用户优惠券删除时间
     *
     * @apiSuccessExample {json} Success-Response 01:
     * HTTP/1.1 200 OK
     * //用户优惠券查询成功
     * [
     *   {
     *     "id": 5,
     *     "name": "品牌优惠券",
     *     "value": 50,
     *     "amount": 10,
     *     "receive_amount": 3,
     *     "limit_fir": 500,
     *     "type": 5,
     *     "remarks": "50元优惠券，满500可用",
     *     "created_at": "2016-05-19 16:54:06",
     *     "start_time": "2016-05-19 00:00:00",
     *     "end_time": "2017-06-30 00:00:00",
     *     "updated_at": "2016-05-27 14:57:42",
     *     "deleted_at": null,
     *     "user_coupon.id": 44,
     *     "user_coupon.use_time": "2016-05-27 14:57:58",
     *     "user_coupon_created_at": null,
     *     "user_coupon_deleted_at": null
     *   },
     *   {
     *     "id": 4,
     *     "name": "店铺优惠券",
     *     "value": 40,
     *     "amount": 100,
     *     "receive_amount": 9,
     *     "limit_fir": 400,
     *     "type": 4,
     *     "remarks": "40元优惠券，满400元即可立减40元",
     *     "created_at": "2016-04-29 11:43:25",
     *     "start_time": "2016-04-29 00:00:00",
     *     "end_time": "2017-06-30 00:00:00",
     *     "updated_at": "2016-05-27 14:57:40",
     *     "deleted_at": null,
     *     "user_coupon.id": 43,
     *     "user_coupon.use_time": "2016-07-01 10:58:00",
     *     "user_coupon_created_at": null,
     *     "user_coupon_deleted_at": null
     *   },
     *   {
     *     "id": 3,
     *     "name": "分类优惠券",
     *     "value": 30,
     *     "amount": 100,
     *     "receive_amount": 9,
     *     "limit_fir": 300,
     *     "type": 3,
     *     "remarks": "30元优惠券,满300元即可立减30元",
     *     "created_at": "2016-04-29 11:41:54",
     *     "start_time": "2016-04-29 00:00:00",
     *     "end_time": "2017-06-30 00:00:00",
     *     "updated_at": "2016-05-27 14:57:39",
     *     "deleted_at": null,
     *     "user_coupon.id": 42,
     *     "user_coupon.use_time": null,
     *     "user_coupon_created_at": null,
     *     "user_coupon_deleted_at": null
     *   },
     *   {
     *     "id": 2,
     *     "name": "商品优惠券",
     *     "value": 20,
     *     "amount": 100,
     *     "receive_amount": 16,
     *     "limit_fir": 200,
     *     "type": 2,
     *     "remarks": "20元红包",
     *     "created_at": "2016-04-29 11:35:01",
     *     "start_time": "2016-04-29 00:00:00",
     *     "end_time": "2017-06-30 00:00:00",
     *     "updated_at": "2016-05-27 14:57:37",
     *     "deleted_at": null,
     *     "user_coupon.id": 41,
     *     "user_coupon.use_time": "2016-06-30 16:43:55",
     *     "user_coupon_created_at": null,
     *     "user_coupon_deleted_at": null
     *   },
     *   {
     *     "id": 1,
     *     "name": "全场通用优惠券",
     *     "value": 10,
     *     "amount": 50,
     *     "receive_amount": 8,
     *     "limit_fir": 100,
     *     "type": 1,
     *     "remarks": "测试优惠券",
     *     "created_at": "2016-04-26 19:09:50",
     *     "start_time": "2016-04-26 00:00:00",
     *     "end_time": "2016-07-15 00:00:00",
     *     "updated_at": "2016-07-11 09:19:55",
     *     "deleted_at": null,
     *     "user_coupon.id": 45,
     *     "user_coupon.use_time": null,
     *     "user_coupon_created_at": null,
     *     "user_coupon_deleted_at": null
     *   }
     * ]
     *
     * @apiSuccessExample {json} Success-Response 02:
     * HTTP/1.1 200 OK
     * //用户优惠券查询成功
     * []
     *
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //用户优惠券查询失败
     * {
     *   "message": "用户id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202
     * //用户优惠券查询失败
     * {
     *   "message": "查询失败"
     * }
     * @return \Illuminate\Http\Response
     */
    public function getCoupon(Request $request, CouponService $couponService)
    {

        $user_id = $request->apiUser->get()->id;

        $coupon_status = $request->coupon_status;


        if (empty($user_id)) {

            throw new ApiInputErrorException("用户id不能为空");
        }//end if()


        try {

            $user_coupon = $couponService->getCouponByStatus($user_id, $coupon_status);

            return response()->json($user_coupon->toArray());

        } catch (CouponServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }

    }


    /**
     * @api {post} /coupon/receive_coupon 用户领取优惠券
     * @apiName Coupon/receive_coupon
     * @apiGroup Coupon
     * @apiDescription 用户领取优惠券.
     *
     * @apiParam {Number} coupon_id  优惠券id.
     *
     * @apiSuccess {Object[]}  Object                       返回的用户优惠券信息
     * @apiSuccess {Number}  Object.result                  用户优惠券表主键id
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //用户领取优惠券成功
     * {
     *   "result": 46
     * }
     *
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //用户领取优惠券失败
     * {
     *   "message": "用户id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //用户领取优惠券失败
     * {
     *   "message": "优惠券id不能为空"
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 202
     * //用户领取优惠券失败
     * {
     *   "message": "用户id不能为空"
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 202
     * //用户领取优惠券失败
     * {
     *   "message": "优惠券id不能为空"
     * }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 202
     * //用户领取优惠券失败
     * {
     *   "message": "优惠券不存在"
     * }
     * @apiErrorExample Error-Response 06:
     * HTTP/1.1 202
     * //用户领取优惠券失败
     * {
     *   "message": "该优惠券已被领完"
     * }
     * @apiErrorExample Error-Response 07:
     * HTTP/1.1 202
     * //用户领取优惠券失败
     * {
     *   "message": "优惠券已过期"
     * }
     * @apiErrorExample Error-Response 08:
     * HTTP/1.1 202
     * //用户领取优惠券失败
     * {
     *   "message": "该优惠券已经领取过了"
     * }
     * @apiErrorExample Error-Response 09:
     * HTTP/1.1 202
     * //用户领取优惠券失败
     * {
     *   "message": "领取优惠券失败"
     * }
     */
    public function receiveCoupon(Request $request, CouponService $couponService){

        $user_id = $request->apiUser->get()->id;

        $coupon_id = $request->coupon_id;


        if (empty($user_id)) {

            throw new ApiInputErrorException("用户id不能为空");
        }//end if()

        if (empty($coupon_id)) {

            throw new ApiInputErrorException("优惠券id不能为空");
        }//end if()

        try {

            $user_coupon_id = $couponService->receiveCoupon($user_id, $coupon_id);

            return response()->json(['result'=>$user_coupon_id]);

        } catch (CouponServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }


    }

}
